package com.px.chapter_Dec_1.Gui;

import java.util.Stack;

public  class Count {
    public static int  Jsq(String target) {
        String[] yi=target.split("[-+]+");
        String[] uu=target.split("[0-9*/]+");
        Stack<String> number=new Stack<>();
        Stack<String> word=new Stack<>();
        for (int a=yi.length-1;a>=0;a--){
            if (yi[a].matches(".*[*/]+.*")) {
                String[] y=yi[a].split("[*/]+");
                String[] u=yi[a].split("[0-9]+");
                Stack<String> number1=new Stack<>();
                Stack<String> word1=new Stack<>();
                for (int aa=y.length-1;aa>=0;aa--){
                    number1.push(y[aa]);
                }
                for (int aa=u.length-1;aa>=0;aa--) {
                    word1.push(u[aa]);
                }
                int o=0;
                word1.pop();
                while (!word1.empty()){
                    if (word1.peek().equals("*")){
                        o=Integer.valueOf(number1.pop())*Integer.valueOf(number1.pop());
                        number1.push(String.valueOf(o));
                        word1.pop();
                    }else if (word1.peek().equals("/")){
                        o=Integer.valueOf(number1.pop())/Integer.valueOf(number1.pop());
                        number1.push(String.valueOf(o));
                        word1.pop();
                    }
                }
                yi[a]=String.valueOf(o);

            }
            number.push(yi[a]);
        }
        for (int c=uu.length-1;c>=0;c--){
            word.push(uu[c]);
        }
        word.pop();
        int o=0;
        while (!word.empty()){
            if (word.peek().equals("+")){
                o=Integer.valueOf(number.pop())+Integer.valueOf(number.pop());
                number.push(String.valueOf(o));
                word.pop();
            }else if (word.peek().equals("-")){
                o=Integer.valueOf(number.pop())-Integer.valueOf(number.pop());
                number.push(String.valueOf(o));
                word.pop();
            }
        }
        return o;
    }
}
